package org.dinnermate.gui.trackers;

import java.sql.SQLException;

import org.dinnermate.staff.ShiftToStaff;
import org.dinnermate.staff.StaffMember;

import net.java.ao.EntityManager;

public class StaffPaymentTracker
{
	String[][] payments;
	StaffMember[] staffMembers;
	ShiftToStaff[] shifts;
	int staffMembersWithShifts;
	
	public StaffPaymentTracker(EntityManager em){
		try
		{
			staffMembers = em.find(StaffMember.class);
			shifts = em.find(ShiftToStaff.class);
			payments = new String[staffMembers.length][4];
			staffMembersWithShifts = 0;
			//Initialise the third column of payments with 0's
			for(int i = 0; i < payments.length; i++){
				payments[i][0] = null;
				payments[i][2] = "0";
			}
			populateArray();
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void populateArray(){
		calculateHours();
		for(int i = 0; i < payments.length && i < staffMembersWithShifts; i++){
			payments[i][3] = (Float.parseFloat(payments[i][1].substring(1)) * Integer.parseInt(payments[i][2])) + "";
		}
	}
	
	public void calculateHours(){
		int j;
		for(int i = 0; i < shifts.length; i++){
			for(j = 0; j < staffMembers.length; j++){
				if(payments[j][0] != null && payments[j][0].equals(shifts[i].getStaffMember().getFullName())){
					break;
				}
				else if(payments[j][0] == null){
					payments[j][0] = shifts[i].getStaffMember().getFullName();
					payments[j][1] = "$" + shifts[i].getStaffMember().getWage();
					staffMembersWithShifts++;
					break;
				}
			}
			payments[j][2] = (Integer.parseInt(payments[j][2]) + 8) + "";
		}
	}
	
	public String[][] getStaffPaymentArray(){
		return payments;
	}
}
